package cn.sangyy.auth.service.impl;

import cn.sangyy.auth.domain.Role;
import cn.sangyy.auth.mapper.RoleMapper;
import cn.sangyy.auth.query.RoleQuery;
import cn.sangyy.auth.service.IRoleService;
import cn.sangyy.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * @author SangYY
 * @date: 2023/05/17/16:08
 */
@Service
public class RoleServiceImpl implements IRoleService {


    @Autowired
    private RoleMapper roleMapper;

    @Override
    public boolean insert(Role role) {
        int num = roleMapper.getRoleByName(role.getName());
        if(num > 0){
            throw new RuntimeException("角色已存在！");
        }
        int insert = roleMapper.insert(role);
        return insert == 1;
    }

    @Override
    public boolean deleteById(Serializable id) {
        int i = roleMapper.deleteById(id);
        return i == 1;
    }

    @Override
    public boolean update(Role role) {
        int update = roleMapper.update(role);
        return update == 1;
    }

    @Override
    public Role getRoleById(Serializable id) {
        return roleMapper.getRoleById(id);
    }

    @Override
    public List<Role> getAllRole() {
        return roleMapper.getAllRole();
    }

    @Override
    public PageList<Role> getRolePageByKeyword(RoleQuery roleQuery) {
        long totals = roleMapper.getTotals(roleQuery);
        if(totals > 0){
            List<Role> rows = roleMapper.getRows(roleQuery);
            return new PageList<>(totals,rows);
        }
        return new PageList<>();
    }

    @Override
    public void batchDelete(List<Long> ids) {
        roleMapper.batchDelete(ids);
    }
}
